package com.ruoyi.work.service.impl;

import java.util.*;

import com.alibaba.fastjson2.util.BeanUtils;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.system.api.domain.SysDept;
import com.ruoyi.work.domain.BwClass;
import com.ruoyi.work.domain.JyImage;
import com.ruoyi.work.domain.JyPicture;
import com.ruoyi.work.domain.vo.*;
import com.ruoyi.work.mapper.JyTailAfterMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.work.mapper.JyStudentCorporationMapper;
import com.ruoyi.work.domain.JyStudentCorporation;
import com.ruoyi.work.service.IJyStudentCorporationService;

/**
 * 学生公司中间Service业务层处理
 *
 * @author ruoyi
 * @date 2024-06-06
 */
@Service
public class JyStudentCorporationServiceImpl implements IJyStudentCorporationService {
    @Autowired
    private JyStudentCorporationMapper jyStudentCorporationMapper;

    @Autowired
    private JyTailAfterMapper jyTailAfterMapper;



    /**
     * 查询学生公司中间
     *
     * @param id 学生公司中间主键
     * @return 学生公司中间
     */
    @Override
    public JyStudentCorporation selectJyStudentCorporationById(Long id) {
        return jyStudentCorporationMapper.selectJyStudentCorporationById(id);
    }

    /**
     * 查询学生公司中间列表
     *
     * @param jyStudentCorporation 学生公司中间
     * @return 学生公司中间
     */
    @Override
    public List<JyStudentCorporation> selectJyStudentCorporationList(JyStudentCorporation jyStudentCorporation) {
        return jyStudentCorporationMapper.selectJyStudentCorporationList(jyStudentCorporation);
    }

    /**
     * 新增学生公司中间
     *
     * @param jyStudentCorporation 学生公司中间
     * @return 结果
     */
    @Override
    public int insertJyStudentCorporation(JyStudentCorporation jyStudentCorporation) {
        return jyStudentCorporationMapper.insertJyStudentCorporation(jyStudentCorporation);
    }



    /**
     * 修改学生公司中间
     *
     * @param bwStudentVo 学生公司中间
     * @return 结果
     */
    @Override
    public R updateJyStudentCorporation(BwStudentVo bwStudentVo) {

        BwClass bwClass = jyStudentCorporationMapper.selectManageIdClass(bwStudentVo.getManageId());
        if (bwClass == null){
            return R.fail(500,"该项目经理还未授课");
        }
        bwStudentVo.setClassId(bwClass.getId());
        bwStudentVo.setCollegeId(bwClass.getCollegeId());
        bwStudentVo.setSchoolId(bwClass.getSchoolId());
        bwStudentVo.setUpdateTime(new Date());
        jyStudentCorporationMapper.updateStudentInfo(bwStudentVo);
        //修改成功后将简历表中的手机号也修改了
        jyStudentCorporationMapper.updateInformation(bwStudentVo.getId(),bwStudentVo.getStuMobile());
        return R.ok();
    }

    /**
     * 批量删除学生公司中间
     *
     * @param ids 需要删除的学生公司中间主键
     * @return 结果
     */
    @Override
    public int deleteJyStudentCorporationByIds(Long[] ids) {
        return jyStudentCorporationMapper.deleteJyStudentCorporationByIds(ids);
    }

    /**
     * 删除学生公司中间信息
     *
     * @param id 学生公司中间主键
     * @return 结果
     */
    @Override
    public int deleteJyStudentCorporationById(Long id) {
        return jyStudentCorporationMapper.deleteJyStudentCorporationById(id);
    }

    @Override
    public List<BwStudentVo> selectBwStudentInfoList(BwStudentVo bwStudentVo) {

        return jyStudentCorporationMapper.selectBwStudentInfoList(bwStudentVo);
    }

    @Override
    public R selectDeptList() {
        List<DeptClassVo> deptClassVos = new ArrayList<>();
        List<SysDept> deptList = jyStudentCorporationMapper.selectDeptList();
        for (SysDept sysDept : deptList) {
            DeptClassVo deptClassVo = new DeptClassVo(sysDept.getDeptId(), sysDept.getDeptName());

            List<BwClass> bwClasses = jyStudentCorporationMapper.selectClassList(sysDept.getDeptId());
            for (BwClass bwClass : bwClasses) {
                Map<String, Object> map = new HashMap<>();
                map.put("value", bwClass.getId());
                map.put("label", bwClass.getClassName());

                deptClassVo.addChildren(map);
            }
            deptClassVos.add(deptClassVo);
        }
        return R.ok(deptClassVos);
    }

    @Override
    public R reportBack(StudentCorporationVo studentCorporationVo) {
        JyStudentCorporation jyStudentCorporation = new JyStudentCorporation();

        jyStudentCorporation.setResignationTime(studentCorporationVo.getResignationTime());
        jyStudentCorporation.setReportResignationTime(studentCorporationVo.getReportResignationTime());
        jyStudentCorporation.setId(studentCorporationVo.getId());
        jyStudentCorporationMapper.updateJyStudentCorporation(jyStudentCorporation);

        jyStudentCorporationMapper.updateStudent(studentCorporationVo.getStudentId());

        return R.ok();
    }

    @Override
    public R reportEntry(StudentCorporationVo studentCorporationVo) {
        TbCorporationVo tbCorporationVo =  jyStudentCorporationMapper.selectCorporation(studentCorporationVo.getCorporationName(),studentCorporationVo.getCorporationAddress());
        if (tbCorporationVo!= null){
            studentCorporationVo.setCorporationId(tbCorporationVo.getId());
        }else {
            TbCorporationVo tbCorporationVo1 = new TbCorporationVo();
            tbCorporationVo1.setCorporationName(studentCorporationVo.getCorporationName());
            tbCorporationVo1.setCorporationAddress(studentCorporationVo.getCorporationAddress());
            jyStudentCorporationMapper.insertCorporation(tbCorporationVo1);
            studentCorporationVo.setCorporationId(tbCorporationVo1.getId());
        }
        if (studentCorporationVo.getIsSubsidy() == null){
            studentCorporationVo.setIsSubsidy("否");
        }
        Integer code = jyStudentCorporationMapper.insertJyStudentCorporation1(studentCorporationVo);
        jyStudentCorporationMapper.updateStu(studentCorporationVo.getStudentId());

        if (!studentCorporationVo.getImageLists().isEmpty()){
        for (String imageList : studentCorporationVo.getImageLists()) {
            JyImage jyImage = new JyImage(null,studentCorporationVo.getId(),imageList,new Date());
            jyStudentCorporationMapper.insertImages(jyImage);
        }
        }
        return R.ok();
    }

    @Override
    public R reportDown(TailAfterVo tailAfterVo) {
        //添加就业跟踪
        jyTailAfterMapper.insertJyTailAfter1(tailAfterVo);

        //添加就业跟踪材料
        if (!tailAfterVo.getPictureLists().isEmpty()){
            for (String pictureList : tailAfterVo.getPictureLists()) {
                JyPicture jyPicture = new JyPicture(null, tailAfterVo.getId(), pictureList, new Date());
                jyTailAfterMapper.insertPicture(jyPicture);
            }
        }
        return R.ok();
    }

    @Override
    public R studentById(Long id) {
        BwStudentVo bwStudentVo = jyStudentCorporationMapper.studentById(id);
        return R.ok(bwStudentVo);
    }
}
